package com.mla.fastdfs.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mla.fastdfs.commom.Result;
import com.mla.fastdfs.dto.RolePermissionDto;
import com.mla.fastdfs.dto.RoleSaveDto;
import com.mla.fastdfs.dto.UserRoleSaveDto;
import com.mla.fastdfs.mapper.RoleMapper;
import com.mla.fastdfs.mapper.RolePermissionMapper;
import com.mla.fastdfs.mapper.UserRoleMapper;
import com.mla.fastdfs.pojo.Role;
import com.mla.fastdfs.pojo.RolePermission;
import com.mla.fastdfs.pojo.User;
import com.mla.fastdfs.pojo.UserRole;
import com.mla.fastdfs.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author Jun
 * @version v1.0
 * @date 2020-08-17
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {
    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private RolePermissionMapper rolePermissionMapper;
    @Autowired
    private UserRoleMapper userRoleMapper;


    public void insert(Role record) {
        record.setInsertTime(new Date());
        roleMapper.insert(record);
    }


    public void update(Role record) {
        record.setUpdateTime(new Date());
        roleMapper.updateById(record);
    }


    public void del(Integer id) {
        roleMapper.deleteById(id);
    }


    public Role selectByPrimaryKey(Integer id) {
        return roleMapper.selectById(id);
    }


    public List<Role> getList(Role record) {
        return roleMapper.selectByRecord(record);
    }


    public PageInfo<Role> pageList(Role record, Integer pageNo, Integer pageSize) {
        PageHelper.startPage(pageNo, pageSize);
        List<Role> list = roleMapper.selectByRecord(record);
        return new PageInfo<>(list);
    }

    public Result saveRole(RoleSaveDto record) {
        //User user = (User) SecurityUtils.getSubject().getPrincipal();
        User user = new User();
        List<Role> repeat = roleMapper.checkRepeat(record);
        if (repeat.size() > 0) {
            return Result.error("角色、角色编号存在重复");
        }
        Role role = new Role();
        role.setCode(record.getCode());
        role.setRoleName(record.getRoleName());
        role.setDescpt(record.getDescpt());
        role.setInsertUid(user.getId());
        if (record.getId() == null) {
            insert(role);
        } else {
            role.setId(record.getId());
            update(role);
        }
        if (record.getPermissions() != null && record.getPermissions().size() > 0) {
            rolePermissionMapper.delByRoleId(role.getId());
            for (Integer id : record.getPermissions()) {
                rolePermissionMapper.insert(new RolePermission().setRoleId(role.getId()).setPermitId(id));
            }
        }
        return Result.ok();
    }

    public Result userRole(UserRoleSaveDto record) {
        UserRole userRole = new UserRole();
        userRole.setUserId(record.getUserId());
        List<UserRole> list = userRoleMapper.selectByRecord(userRole);
        if (list.size() > 0) {
            userRoleMapper.deleteById(record.getUserId());
        }
        userRole.setRoleId(record.getRoleId());
        userRoleMapper.insert(userRole);
        return Result.ok();
    }

    public Result roleInfo(RolePermissionDto record) {
        List<RolePermissionDto> list = rolePermissionMapper.selectByPermission(record);
        return Result.ok(list);
    }
}